# coding:utf-8

# Create your views here.
from django.template import RequestContext, loader
from django.http import HttpResponse
import os
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from signing.models import Order
from signing.models import Status
from signing.models import Item
from django.contrib.auth import  authenticate
from django.http import HttpResponseRedirect


def index(request):

    pre_status = Status.objects.get(name='已预约')
    sign_status = Status.objects.get(name='已签约')
    back_status = Status.objects.get(name='已退单')
    completed_status = Status.objects.get(name='已完成')

    pre_order_list = Order.objects.filter(status = pre_status)
    sign_order_list = Order.objects.filter(status = sign_status)
    back_order_list = Order.objects.filter(status = back_status)
    completed_order_list = Order.objects.filter(status = completed_status)

    number = {'pre':len(pre_order_list),
              'sign':len(sign_order_list),
              'back':len(back_order_list),
              'completed':len(completed_order_list)}

    order_list = Order.objects.all()
    context = RequestContext(request, {
        'order_list': order_list,
        'number':number,

    })
    template = loader.get_template('index.html')
    return HttpResponse(template.render(context))


def login(request):
    return render_to_response('login.html')
def dologin(request):
    print request.POST
    username = request.POST[u'username']
    password = request.POST[u'password']
    user = authenticate(username=username, password=password)
    print username
    if user is not None:
        print user
        return HttpResponseRedirect('/')
    else:
        return HttpResponseRedirect('/login/')
    return  HttpResponse('xx')
def order_detail(request):


    pre_status = Status.objects.get(name='已预约')
    sign_status = Status.objects.get(name='已签约')
    back_status = Status.objects.get(name='已退单')
    completed_status = Status.objects.get(name='已完成')

    pre_order_list = Order.objects.filter(status = pre_status)
    sign_order_list = Order.objects.filter(status = sign_status)
    back_order_list = Order.objects.filter(status = back_status)
    completed_order_list = Order.objects.filter(status = completed_status)

    number = {'pre':len(pre_order_list),
              'sign':len(sign_order_list),
              'back':len(back_order_list),
              'completed':len(completed_order_list)}

    orderid = request.GET['id']
    order = Order.objects.get(orderID = orderid)
    item_list = Item.objects.filter(order=order)

    context = RequestContext(request, {
        'order': order,
        'item_list':item_list,
         'number':number,
    })
    template = loader.get_template('orderdetail.html')
    return HttpResponse(template.render(context))

def order_list(request):
    # 订单状态
    print 'xxxx'
    pre_status = Status.objects.get(name='已预约')
    sign_status = Status.objects.get(name='已签约')
    back_status = Status.objects.get(name='已退单')
    completed_status = Status.objects.get(name='已完成')

    pre_order_list = Order.objects.filter(status = pre_status)
    sign_order_list = Order.objects.filter(status = sign_status)
    back_order_list = Order.objects.filter(status = back_status)
    completed_order_list = Order.objects.filter(status = completed_status)

    number = {'pre':len(pre_order_list),
              'sign':len(sign_order_list),
              'back':len(back_order_list),
              'completed':len(completed_order_list)}

    object =  request.GET['status']

    name = ''
    if object == 'pre':
        name = '已预约'
    if object == 'sign':
        name = '已签约'
    if object == 'back':
        name = '已退单'
    if object == 'completed':
        name = '已完成'


    status = Status.objects.get(name=name)

    order_list = Order.objects.filter(status = status)

    context = RequestContext(request, {
        'order_list': order_list,
        'number':number,
    })
    template = loader.get_template('orderlist.html')
    return HttpResponse(template.render(context))